#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

year = 2014

infile = open("D:/data/mesh2/desc" + str(year) + ".txt", "r")
outfile = open("D:/data/mesh2/desc" + str(year) + ".txt.correctformat", "w")
line_count = 0
title_line = ""
for line in infile:
    line_count += 1
    line = line.rstrip()
    if line_count % 2 != 0:
        title_line = line
    else:
        if title_line != "":
            split_title_line = title_line.split("\t")
            type = split_title_line[0]
            id = split_title_line[1]
            name = split_title_line[2]
            outfile.write(type + "\t" + id + "." + str(year) + "\t" + name + "\n")
            outfile.flush()
            if line != "":
                splitline = line.split("\t")
                for no in xrange(len(splitline)):
                    outfile.write(type + "\t" + id + "." + str(year) + "." + str(no + 1) + "\t" + splitline[no] + "\n")
                    outfile.flush()
infile.close()
outfile.close()

infile = open("D:/data/mesh2/qual" + str(year) + ".txt", "r")
outfile = open("D:/data/mesh2/qual" + str(year) + ".txt.correctformat", "w")
line_count = 0
title_line = ""
for line in infile:
    line_count += 1
    line = line.rstrip()
    if line_count % 2 != 0:
        title_line = line
    else:
        if title_line != "":
            split_title_line = title_line.split("\t")
            type = split_title_line[0]
            id = split_title_line[1]
            name = split_title_line[2]
            outfile.write(type + "\t" + id + "." + str(year) + "\t" + name + "\n")
            outfile.flush()
            if line != "":
                splitline = line.split("\t")
                for no in xrange(len(splitline)):
                    outfile.write(type + "\t" + id + "." + str(year) + "." + str(no + 1) + "\t" + splitline[no] + "\n")
                    outfile.flush()
infile.close()
outfile.close()
